package com.sky.service.impl;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.*;
import com.sky.mapper.DishMapper;

import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j

public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 添加商品到购物车
     * @param shoppingCartDTO
     */
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        //创建时间
        shoppingCart.setCreateTime(LocalDateTime.now());
        //用户id
        Long userId = (Long) redisTemplate.opsForValue().get("USER_ID");

        shoppingCart.setUserId(userId);
        //拷贝
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //判断购物车是否有该商品
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list!=null&&!list.isEmpty()){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumber(cart);
            return;
        }

        // name image user_id dish_id setmeal_id dish_flavor number amount create_time
        //先判断该商品是菜品还是套餐
        if(shoppingCartDTO.getDishId()!=null){
            //菜品
            //根据dishId在dish表找到菜品,找到并拷贝给ShoppingCart表
            Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
            List<DishFlavor> dishFlavors = dishMapper.selectFlavorByDishId(shoppingCartDTO.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setNumber(1);


            //放进数据库
            shoppingCartMapper.save(shoppingCart);
            return;

        }
        //套餐
        Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
        //拷贝
        shoppingCart.setName(setmeal.getName());
        shoppingCart.setImage(setmeal.getImage());
        shoppingCart.setAmount(setmeal.getPrice());
        shoppingCart.setNumber(1);
        //放进数据库
        shoppingCartMapper.save(shoppingCart);


    }

    @Override
    public List<ShoppingCart> list(Long userId) {
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.listById(userId);
        return shoppingCarts;
    }

    @Override
    public void delete(Long userId) {
        shoppingCartMapper.deleteById(userId);
    }


    public void sub(Long userId, ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        //拷贝
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //判断购物车是否有该商品
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list!=null&&!list.isEmpty()){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.sub(userId,cart);
            if(cart.getNumber()==0){
                shoppingCartMapper.deleteByDishIdOrSetmealId(userId,cart);
            }
            return;
        }

    }
}
